*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {
	
*******************************************************************************
* Load data
*******************************************************************************

use dataset_gallup, clear

*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

*******************************************************************************
* Make lags
*******************************************************************************

local lags
forvalues i = 1993(1)2013 {
	egen conf`i'm = mean(conf_) if year == `i', by(target)
	egen conf`i' = mean(conf`i'm), by(target)
	drop conf`i'm
	local lags `lags' conf`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "police"

*******************************************************************************
* Synth
*******************************************************************************

synth conf_ `lags', trunit(`r(mean)') trperiod(2014) keep(tmp, replace)
use tmp, clear
erase tmp.dta

*******************************************************************************
* Get table of weights
*******************************************************************************

keep if !mi(_W_)
drop _Y_* _time
rename _W_ Weight_`subgroup'
save tmp_`subgroup', replace

*******************************************************************************
* Close subgroup loop
*******************************************************************************

}

*******************************************************************************
* Combine tables
*******************************************************************************

use tmp_All, clear
merge 1:1 _Co_Number using tmp_Democrat
drop _m
merge 1:1 _Co_Number using tmp_Republican
drop _m

erase tmp_All.dta
erase tmp_Democrat.dta
erase tmp_Republican.dta

*******************************************************************************
* Format table
*******************************************************************************

decode _Co_, gen(target)
replace target = subinstr(target, "_", " ", 1)
drop _Co_
order target

replace target = proper(target)
replace target = "Supreme Court" if target == "Sup Ct"
replace target = "TV News" if target == "Tv News"

*******************************************************************************
* Bold non-zero entries
*******************************************************************************

gen any_nonzero = Weight_A > 0 | Weight_D > 0 | Weight_R > 0
tostring Weight*, replace force format(%9.0g)
foreach x in target Weight_A Weight_D Weight_R {
	replace `x' = "\bf{" + `x' + "}" if any_nonzero == 1 & `x' != "0"
	replace `x' = "" if `x' == "0"
}
drop any_nonzero 

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

listtex 
	using "table_gallup_sc.tex"
	,
	replace
	type rstyle(tabular)
	head(
		\begin{tabular*}
		{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{4}{l}}
		\toprule 
		Series & Weight All & Weight Dem & Weight Rep
		\\
		\midrule
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr

*******************************************************************************
* End
*******************************************************************************
